home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / rect.el.z / rect.el
Encoding:
Text File  |  1998-05-21  |  9.1 KB  |  257 lines

  1. ;;; rect.el --- rectangle functions for XEmacs.
  2.  
  3. ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: internal
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. ;; 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; This package provides the operations on rectangles that are ocumented
  30. ;; in the XEmacs Reference Manual.
  31.  
  32. ;;; Code:
  33.  
  34. ;; XEmacs: extra-args
  35. (defun operate-on-rectangle (function start end coerce-tabs &rest extra-args)
  36.   "Call FUNCTION for each line of rectangle with corners at START, END.
  37. If COERCE-TABS is non-nil, convert multi-column characters
  38. that span the starting or ending columns on any line
  39. to multiple spaces before calling FUNCTION.
  40. FUNCTION is called with three arguments:
  41.  position of start of segment of this line within the rectangle,
  42.  number of columns that belong to rectangle but are before that position,
  43.  number of columns that belong to rectangle but are after point.
  44. Point is at the end of the segment of this line within the rectangle."
  45.   (let (startcol startlinepos endcol endlinepos)
  46.     (save-excursion
  47.      (goto-char start)
  48.      (setq startcol (current-column))
  49.      (beginning-of-line)
  50.      (setq startlinepos (point)))
  51.     (save-excursion
  52.      (goto-char end)
  53.      (setq endcol (current-column))
  54.      (forward-line 1)
  55.      (setq endlinepos (point-marker)))
  56.     (if (< endcol startcol)
  57.     ;; XEmacs
  58.     (let ((tem startcol))
  59.       (setq startcol endcol endcol tem)))
  60.     (save-excursion
  61.       (goto-char startlinepos)
  62.       (while (< (point) endlinepos)
  63.     (let (startpos begextra endextra)
  64.       (move-to-column startcol coerce-tabs)
  65.       (setq begextra (- (current-column) startcol))
  66.       (setq startpos (point))
  67.       (move-to-column endcol coerce-tabs)
  68.       (setq endextra (- endcol (current-column)))
  69.       (if (< begextra 0)
  70.           (setq endextra (+ endextra begextra)
  71.             begextra 0))
  72.       (if (< endextra 0) (setq endextra 0))
  73.       (apply function startpos begextra endextra extra-args))
  74.     (forward-line 1)))
  75.     (- endcol startcol)))
  76.  
  77. (defun delete-rectangle-line (startdelpos ignore ignore)
  78.   (delete-region startdelpos (point)))
  79.  
  80. ;; XEmacs: added lines arg
  81. (defun delete-extract-rectangle-line (startdelpos begextra endextra lines)
  82.   (save-excursion
  83.    (extract-rectangle-line startdelpos begextra endextra lines))
  84.   (delete-region startdelpos (point)))
  85.  
  86. ;; XEmacs: added lines arg
  87. (defun extract-rectangle-line (startdelpos begextra endextra lines)
  88.   (let ((line (buffer-substring startdelpos (point)))
  89.     (end (point)))
  90.     (goto-char startdelpos)
  91.     (while (search-forward "\t" end t)
  92.       (let ((width (- (current-column)
  93.               (save-excursion (forward-char -1)
  94.                       (current-column)))))
  95.     (setq line (concat (substring line 0 (- (point) end 1))
  96.                (spaces-string width)
  97.                (substring line (+ (length line) (- (point) end)))))))
  98.     (if (or (> begextra 0) (> endextra 0))
  99.     (setq line (concat (spaces-string begextra)
  100.                line
  101.                (spaces-string endextra))))
  102.     (setcdr lines (cons line (cdr lines))))) ; XEmacs
  103.  
  104. (defconst spaces-strings
  105.   (purecopy '["" " " "  " "   " "    " "     " "      " "       " "        "]))
  106.  
  107. (defun spaces-string (n)
  108.   (if (<= n 8) (aref spaces-strings n)
  109.     (let ((val ""))
  110.       (while (> n 8)
  111.     (setq val (concat "        " val)
  112.           n (- n 8)))
  113.       (concat val (aref spaces-strings n)))))
  114.     
  115. ;;;###autoload
  116. (defun delete-rectangle (start end)
  117.   "Delete (don't save) text in rectangle with point and mark as corners.
  118. The same range of columns is deleted in each line starting with the line
  119. where the region begins and ending with the line where the region ends."
  120.   (interactive "r")
  121.   (operate-on-rectangle 'delete-rectangle-line start end t))
  122.  
  123. ;;;###autoload
  124. (defun delete-extract-rectangle (start end)
  125.   "Delete contents of rectangle and return it as a list of strings.
  126. Arguments START and END are the corners of the rectangle.
  127. The value is list of strings, one for each line of the rectangle."
  128.   (let ((lines (list nil))) ; XEmacs change
  129.     (operate-on-rectangle 'delete-extract-rectangle-line
  130.               start end t lines)
  131.     (nreverse (cdr lines))))
  132.  
  133. ;;;###autoload
  134. (defun extract-rectangle (start end)
  135.   "Return contents of rectangle with corners at START and END.
  136. Value is list of strings, one for each line of the rectangle."
  137.   (let ((lines (list nil))) ; XEmacs change
  138.     (operate-on-rectangle 'extract-rectangle-line start end nil lines)
  139.     (nreverse (cdr lines))))
  140.  
  141. ;;;###autoload
  142. (defvar killed-rectangle nil
  143.   "Rectangle for yank-rectangle to insert.")
  144.  
  145. ;;;###autoload
  146. (defun kill-rectangle (start end)
  147.   "Delete rectangle with corners at point and mark; save as last killed one.
  148. Calling from program, supply two args START and END, buffer positions.
  149. But in programs you might prefer to use `delete-extract-rectangle'."
  150.   (interactive "r")
  151.   (if buffer-read-only
  152.       (progn
  153.     (setq killed-rectangle (extract-rectangle start end))
  154.     (barf-if-buffer-read-only)))
  155.   (setq killed-rectangle (delete-extract-rectangle start end)))
  156.  
  157. ;;;###autoload
  158. (defun yank-rectangle ()
  159.   "Yank the last killed rectangle with upper left corner at point."
  160.   (interactive)
  161.   (insert-rectangle killed-rectangle))
  162.  
  163. ;;;###autoload
  164. (defun insert-rectangle (rectangle)
  165.   "Insert text of RECTANGLE with upper left corner at point.
  166. RECTANGLE's first line is inserted at point, its second
  167. line is inserted at a point vertically under point, etc.
  168. RECTANGLE should be a list of strings.
  169. After this command, the mark is at the upper left corner
  170. and point is at the lower right corner."
  171.   (let ((lines rectangle)
  172.     (insertcolumn (current-column))
  173.     (first t))
  174.     (push-mark)
  175.     (while lines
  176.       (or first
  177.       (progn
  178.        (forward-line 1)
  179.        (or (bolp) (insert ?\n))
  180.        (move-to-column insertcolumn t)))
  181.       (setq first nil)
  182.       (insert (car lines))
  183.       (setq lines (cdr lines)))))
  184.  
  185. ;;;###autoload
  186. (defun open-rectangle (start end)
  187.   "Blank out rectangle with corners at point and mark, shifting text right.
  188. The text previously in the region is not overwritten by the blanks,
  189. but instead winds up to the right of the rectangle."
  190.   (interactive "r")
  191.   (operate-on-rectangle 'open-rectangle-line start end nil)
  192.   (goto-char start))
  193.  
  194. (defun open-rectangle-line (startpos begextra endextra)
  195.   ;; Column where rectangle ends.
  196.   (let ((endcol (+ (current-column) endextra))
  197.     whitewidth)
  198.     (goto-char startpos)
  199.     ;; Column where rectangle begins.
  200.     (let ((begcol (- (current-column) begextra)))
  201.       (skip-chars-forward " \t")
  202.       ;; Width of whitespace to be deleted and recreated.
  203.       (setq whitewidth (- (current-column) begcol)))
  204.     ;; Delete the whitespace following the start column.
  205.     (delete-region startpos (point))
  206.     ;; Open the desired width, plus same amount of whitespace we just deleted.
  207.     (indent-to (+ endcol whitewidth))))
  208.  
  209. ;;;###autoload
  210. (defun string-rectangle (start end string)
  211.   "Insert STRING on each line of the region-rectangle, shifting text right.
  212. The left edge of the rectangle specifies the column for insertion.
  213. This command does not delete or overwrite any existing text.
  214.  
  215. Called from a program, takes three args; START, END and STRING."
  216.   (interactive "r\nsString rectangle: ")
  217.   (operate-on-rectangle 'string-rectangle-line start end t string)) ; XEmacs
  218.  
  219. ;; XEmacs: add string arg
  220. (defun string-rectangle-line (startpos begextra endextra string)
  221.   (let (whitespace)
  222.     (goto-char startpos)
  223.     ;; Compute horizontal width of following whitespace.
  224.     (let ((ocol (current-column)))
  225.       (skip-chars-forward " \t")
  226.       (setq whitespace (- (current-column) ocol)))
  227.     ;; Delete the following whitespace.
  228.     (delete-region startpos (point))
  229.     ;; Insert the desired string.
  230.     (insert string)
  231.     ;; Insert the same width of whitespace that we had before.
  232.     (indent-to (+ (current-column) whitespace))))
  233.  
  234. ;;;###autoload
  235. (defun clear-rectangle (start end)
  236.   "Blank out rectangle with corners at point and mark.
  237. The text previously in the region is overwritten by the blanks.
  238. When called from a program, requires two args which specify the corners."
  239.   (interactive "r")
  240.   (operate-on-rectangle 'clear-rectangle-line start end t))
  241.  
  242. (defun clear-rectangle-line (startpos begextra endextra)
  243.   ;; Find end of whitespace after the rectangle.
  244.   (skip-chars-forward " \t")
  245.   (let ((column (+ (current-column) endextra)))
  246.     ;; Delete the text in the rectangle, and following whitespace.
  247.     (delete-region (point)
  248.                    (progn (goto-char startpos)
  249.               (skip-chars-backward " \t")
  250.               (point)))
  251.     ;; Reindent out to same column that we were at.
  252.     (indent-to column)))
  253.  
  254. (provide 'rect)
  255.  
  256. ;;; rect.el ends here
  257.